/*
 * @Date: 2022-03-28 16:09:58
 * @LastEditors: 春贰
 * @gitee: https://gitee.com/chun22222222
 * @github: https://github.com/chun222
 * @Desc:
 * @LastEditTime: 2022-03-28 17:31:19
 * @FilePath: \server\system\controller\sys\sysconfig\sysconfig.go
 */

package sysconfig

import (
	"go2admin_pure/system/core/request"
	"go2admin_pure/system/core/response"
	"go2admin_pure/system/model/CommonModel"
	"go2admin_pure/system/model/DbModel"
	"go2admin_pure/system/model/RequestModel"
	"go2admin_pure/system/service/SysConfigService"
	"github.com/gin-gonic/gin"
)

var SysConfigSer SysConfigService.SysConfigService

/**
 *
 * @api {post} /user/list 用户列表
 * @apiName 用户列表
 * @apiGroup 用户
 * @apiVersion  1.0.0
 *
 * @apiBody  {String}  page
 * @apiBody  {String}  pageSize
 * @apiSampleRequest /user/list
 * @apiHeader {String} Authorization 唯一token
 *
 * @apiParamExample  {json} Request-Example:
 * {
 *    key : "value",
 * }
 *
 *
 * @apiSuccessExample {json} Success-Response:
 *
 {
  "code": 0,
	 "data": {
	 },
	 "msg": "sucess"
 }
 *
 *
*/
func List(c *gin.Context) {
	var r RequestModel.SysConfig
	err, msg := request.Binding(&r, c)
	if err != nil {
		response.FailWithMessage(msg, c)
		return
	}

	if err, list, total := SysConfigSer.List(r); err != nil {
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithData(
			CommonModel.PageResult{
				List:     list,
				Total:    total,
				Page:     r.Page,
				PageSize: r.PageSize,
			}, c)
	}

}

/**
 *
 * @api {post} /user/add 新增用户
 * @apiName 新增用户
 * @apiGroup 用户
 * @apiVersion  1.0.0
 * @apiHeader {String} Authorization 唯一token
 * @apiBody  {String}  apiBody
 * @apiSampleRequest /user/add
 *
 * @apiParamExample  {json} Request-Example:
 *{"login_name":"12s1","User_Name":"asss","password":"ad","email":"sss@qq.com"}
 *
 *
 * @apiSuccessExample {json} Success-Response:
 *
 {
	 "code": 0,
	 "data": 12,  //用户id
	 "msg": "success"
 }
 *
 *
*/
func Add(c *gin.Context) {
	var r DbModel.SysConfig
	err, msg := request.Binding(&r, c)
	if err != nil {
		response.FailWithMessage(msg, c)
		return
	}
	if err = SysConfigSer.Add(&r); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	} else {
		response.OkWithData(r.Name, c)
	}
}

func Edit(c *gin.Context) {
	var r DbModel.SysConfig
	err, msg := request.Binding(&r, c)
	if err != nil {
		response.FailWithMessage(msg, c)
		return
	}

	if err = SysConfigSer.Edit(&r); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	} else {
		response.Ok(c)
	}
}

func Remove(c *gin.Context) {
	var r CommonModel.Names
	err, msg := request.Binding(&r, c)
	if err != nil {
		response.FailWithMessage(msg, c)
		return
	}

	if err = SysConfigSer.Remove(r); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	} else {
		response.Ok(c)
	}
}
